package com.shawn.autoconfig.template;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.tea.TeaException;
import com.shawn.autoconfig.properties.SmsProperties;

/**
 * 短信模板
 * @author lenovo
 * @date 2022/10/23
 */
public class SmsTemplate {
private SmsProperties smsProperties;
public SmsTemplate(SmsProperties smsProperties){
    this.smsProperties=smsProperties;
}
//发送短信
public String sendShortMessage(String phoneNum,String code) throws Exception {
    String accessKeyId = smsProperties.getAccessKeyId();
    String accessKeySecret =smsProperties.getAccessKeySecret();
    com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            // 您的 AccessKey ID
            .setAccessKeyId(accessKeyId)
            // 您的 AccessKey Secret
            .setAccessKeySecret(accessKeySecret);
    // 访问的域名
    config.endpoint = "dysmsapi.aliyuncs.com";
    com.aliyun.dysmsapi20170525.Client client = new Client(config);
    com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
            .setSignName(smsProperties.getSignName())
            .setTemplateCode(smsProperties.getTemplateCode())
            .setPhoneNumbers(phoneNum)
            .setTemplateParam("{\"code\":\""+code+"\"}");
    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse smsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
        SendSmsResponseBody body = smsResponse.getBody();
        return body.getMessage();
}
}
